package com.langhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.langhua.common.R;
import com.langhua.entity.PurchaseRequisition;
import com.langhua.entity.User;
import com.langhua.service.PurchaseRequisitionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/purchaseRequisitions")
@Slf4j
public class PurchaseRequisitionController {

    @Autowired
    private PurchaseRequisitionService purchaseRequisitionService;

    /**
     * 管理员查询有 申请状况 分页查询
     * @param current  当前页
     * @param size     每页条数
     * @param purchaseRequisition   条件查询
     * @return
     */
    @GetMapping("{current}/{size}")
    public R<IPage> getPage(@PathVariable Integer current, @PathVariable Integer size, PurchaseRequisition purchaseRequisition){
        IPage page=new Page(current,size);
        LambdaQueryWrapper<PurchaseRequisition> lqw=new LambdaQueryWrapper<PurchaseRequisition>();
        lqw.like(Strings.isNotEmpty(purchaseRequisition.getFoodsstuffName()),PurchaseRequisition::getFoodsstuffName,purchaseRequisition.getFoodsstuffName());

        IPage page1 = purchaseRequisitionService.page(page,lqw);
        return R.success(page1,"purchaseRequisition分页查询");
    }


    /**
     * 采购员分页查询 个人申请状况
     * @param current  当前页
     * @param size     每页条数
     * @param purchaseRequisition   条件查询
     * @return
     */
    @GetMapping("getByIdNumberAndName/{current}/{size}")
    public R<IPage> getByIdNumberAndName(@PathVariable Integer current, @PathVariable Integer size, PurchaseRequisition purchaseRequisition){
        IPage page=new Page(current,size);
        LambdaQueryWrapper<PurchaseRequisition> lqw=new LambdaQueryWrapper<PurchaseRequisition>();

        lqw.eq(Strings.isNotEmpty(purchaseRequisition.getName()),PurchaseRequisition::getName,purchaseRequisition.getName());
        lqw.eq(Strings.isNotEmpty(purchaseRequisition.getIdNumber()),PurchaseRequisition::getIdNumber,purchaseRequisition.getIdNumber());

        lqw.like(Strings.isNotEmpty(purchaseRequisition.getName()),PurchaseRequisition::getName,purchaseRequisition.getName());

        IPage page1 = purchaseRequisitionService.page(page,lqw);
        return R.success(page1,"purchaseRequisition分页查询");
    }


    /**
     * 查询所有采购申请类型(去重)
     * @return
     */
    @GetMapping
    public R<List<PurchaseRequisition>> getPage(){

        QueryWrapper<PurchaseRequisition> purchaseRequisitionQueryWrapper = new QueryWrapper<>();
        purchaseRequisitionQueryWrapper.select("DISTINCT type");
        List<PurchaseRequisition> list = purchaseRequisitionService.list(purchaseRequisitionQueryWrapper);
        System.out.println("purchaseRequisition查询所有采购申请类型(去重)："+list);
        return R.success(list,"purchaseRequisition查询所有采购申请类型(去重)");
    }

    /**
     * 新增采购申请
     * @param purchaseRequisition
     * @return
     */
    @PostMapping("/add")
    public R<String> save(@RequestBody PurchaseRequisition purchaseRequisition){


        Date date = Calendar.getInstance().getTime();

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-d hh:mm:ss");

        String strDate = dateFormat.format(date);
        purchaseRequisition.setDate(strDate);

        log.info("新增采购申请，采购申请信息：{}",purchaseRequisition.toString());
        System.out.println("新增得id："+purchaseRequisition);
        purchaseRequisition.setStatus("未审核");
        purchaseRequisitionService.save(purchaseRequisition);

        return R.success("新增采购申请成功",null);
    }

    /**
     * 根据id修改采购申请信息()
     * @param purchaseRequisition
     * @return
     */
    @PutMapping("/updatePurchaseRequisition")
    public R<String> updatePurchaseRequisition(@RequestBody PurchaseRequisition purchaseRequisition){

        purchaseRequisitionService.updateById(purchaseRequisition);

        return R.success("采购申请信息修改成功",null);
    }

    /**
     * 通过id删除PurchaseRequisition
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public R<String> deleteById(@PathVariable Long id){
        log.info("删除PurchaseRequisition的id为：{}", id);

        purchaseRequisitionService.removeById(id);
        return R.success(null,"删除成功！");
    }

    /**
     * 通过id修改采购申请状态
     * @return
     */
    @PutMapping("/updatePurchaseRequisitiontStatus")
    public R updateStudentStatus(@RequestBody PurchaseRequisition purchaseRequisition){
        boolean b = purchaseRequisitionService.updateById(purchaseRequisition);
        if (b){
            return R.success(purchaseRequisition,"通过id修改采购申请状态成功");
        }else {
            return R.error("通过id修改采购申请状态失败！");
        }
    }

}
